<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="总登记号" prop="generalAccessionNumber">
        <el-input v-model="formData.generalAccessionNumber" placeholder="请输入总登记号" />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-select v-model="formData.category" placeholder="请选择类别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_CATEGORY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="原名" prop="originalName">
        <el-input v-model="formData.originalName" placeholder="请输入原名" />
      </el-form-item>
      <el-form-item label="封面图片路径" prop="coverImage">
        <UploadImg v-model="formData.coverImage" />
      </el-form-item>
      <el-form-item label="附件信息" prop="attachments">
        <UploadFile v-model="formData.attachments" />
      </el-form-item>
      <el-form-item label="质地类别" prop="materialType">
        <el-select v-model="formData.materialType" placeholder="请选择质地类别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_MATERIAL_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实际数量" prop="actureQuantity">
        <el-input v-model="formData.actureQuantity" placeholder="请输入实际数量" />
      </el-form-item>
      <el-form-item label="质量" prop="quality">
        <el-input v-model="formData.quality" placeholder="请输入质量" />
      </el-form-item>
      <el-form-item label="件数(套)" prop="quantity">
        <el-input v-model="formData.quantity" placeholder="请输入件数(套)" />
      </el-form-item>
      <el-form-item label="级别" prop="grade">
        <el-select v-model="formData.grade" placeholder="请选择级别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_GRADE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来原捐赠单位或捐赠人" prop="sourceExtention">
        <el-input v-model="formData.sourceExtention" placeholder="请输入来原捐赠单位或捐赠人" />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-select v-model="formData.source" placeholder="请选择来源">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="完残状况" prop="conditionStatus">
        <el-select v-model="formData.conditionStatus" placeholder="请选择完残状况">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_CONDITION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保存状态" prop="preservationStatus">
        <el-select v-model="formData.preservationStatus" placeholder="请选择保存状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_PRESERVATION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入馆日期" prop="entryDate">
        <el-date-picker
          v-model="formData.entryDate"
          type="date"
          value-format="x"
          placeholder="选择入馆日期"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="formData.remarks" placeholder="请输入备注" />
      </el-form-item>


      <!--      书画类藏品特有字段-->
      <el-form-item label="画心尺寸-长" prop="paintingLength">
        <el-input v-model="formData.paintingLength" placeholder="请输入画心尺寸-长" />
      </el-form-item>
      <el-form-item label="画心尺寸-宽" prop="paintingWidth">
        <el-input v-model="formData.paintingWidth" placeholder="请输入画心尺寸-宽" />
      </el-form-item>
      <el-form-item label="画心尺寸-高" prop="paintingHeight">
        <el-input v-model="formData.paintingHeight" placeholder="请输入画心尺寸-高" />
      </el-form-item>
      <el-form-item label="装裱尺寸-长" prop="mountingLength">
        <el-input v-model="formData.mountingLength" placeholder="请输入装裱尺寸-长" />
      </el-form-item>
      <el-form-item label="装裱尺寸-宽" prop="mountingWidth">
        <el-input v-model="formData.mountingWidth" placeholder="请输入装裱尺寸-宽" />
      </el-form-item>
      <el-form-item label="装裱尺寸-高" prop="mountingHeight">
        <el-input v-model="formData.mountingHeight" placeholder="请输入装裱尺寸-高" />
      </el-form-item>
      <el-form-item label="内容题材" prop="subjectMatter">
        <el-input v-model="formData.subjectMatter" placeholder="请输入内容题材" />
      </el-form-item>
      <el-form-item label="技法风格" prop="techniqueStyle">
        <el-input v-model="formData.techniqueStyle" placeholder="请输入技法风格" />
      </el-form-item>
      <el-form-item label="款识信息" prop="inscriptionInfo">
        <el-input v-model="formData.inscriptionInfo" placeholder="请输入款识信息" />
      </el-form-item>

      <!--      印章类藏品特有字段-->
      <el-form-item label="形状" prop="shape">
        <el-input v-model="formData.shape" placeholder="请输入形状" />
      </el-form-item>
      <el-form-item label="钮式" prop="knobStyle">
        <el-input v-model="formData.knobStyle" placeholder="请输入钮式" />
      </el-form-item>
      <el-form-item label="印文内容" prop="sealContent">
        <Editor v-model="formData.sealContent" height="150px" />
      </el-form-item>
      <el-form-item label="文字类型" prop="textType">
        <el-select v-model="formData.textType" placeholder="请选择文字类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="印文排列" prop="textArrangement">
        <el-input v-model="formData.textArrangement" placeholder="请输入印文排列" />
      </el-form-item>
      <el-form-item label="边框特征" prop="borderFeature">
        <el-input v-model="formData.borderFeature" placeholder="请输入边框特征" />
      </el-form-item>
      <el-form-item label="印面尺寸" prop="sealSize">
        <el-input v-model="formData.sealSize" placeholder="请输入印面尺寸" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { CollectionManagementApi, CollectionManagementVO } from '@/api/museum/collectionmanagement'

/** 藏品管理 表单 */
defineOptions({ name: 'CollectionManagementForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  generalAccessionNumber: undefined,
  category: undefined,
  name: undefined,
  originalName: undefined,
  coverImage: undefined,
  attachments: undefined,
  materialType: undefined,
  actureQuantity: undefined,
  quality: undefined,
  quantity: undefined,
  grade: undefined,
  sourceExtention: undefined,
  source: undefined,
  conditionStatus: undefined,
  preservationStatus: undefined,
  entryDate: undefined,
  remarks: undefined,
  paintingLength: undefined,
  paintingWidth: undefined,
  paintingHeight: undefined,
  mountingLength: undefined,
  mountingWidth: undefined,
  mountingHeight: undefined,
  subjectMatter: undefined,
  techniqueStyle: undefined,
  inscriptionInfo: undefined,
  shape: undefined,
  knobStyle: undefined,
  sealContent: undefined,
  textType: undefined,
  textArrangement: undefined,
  borderFeature: undefined,
  sealSize: undefined
})
const formRules = reactive({
  generalAccessionNumber: [{ required: true, message: '总登记号不能为空', trigger: 'blur' }],
  category: [{ required: true, message: '类别不能为空', trigger: 'change' }],
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  originalName: [{ required: true, message: '原名不能为空', trigger: 'blur' }],
  coverImage: [{ required: true, message: '封面图片路径不能为空', trigger: 'blur' }],
  materialType: [{ required: true, message: '质地类别不能为空', trigger: 'change' }],
  actureQuantity: [{ required: true, message: '实际数量不能为空', trigger: 'blur' }],
  quality: [{ required: true, message: '质量不能为空', trigger: 'blur' }],
  quantity: [{ required: true, message: '件数(套)不能为空', trigger: 'blur' }],
  grade: [{ required: true, message: '级别不能为空', trigger: 'change' }],
  source: [{ required: true, message: '来源不能为空', trigger: 'change' }],
  conditionStatus: [{ required: true, message: '完残状况不能为空', trigger: 'change' }],
  preservationStatus: [{ required: true, message: '保存状态不能为空', trigger: 'change' }],
  entryDate: [{ required: true, message: '入馆日期不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CollectionManagementApi.getCollectionManagement(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CollectionManagementVO
    if (formType.value === 'create') {
      await CollectionManagementApi.createCollectionManagement(data)
      message.success(t('common.createSuccess'))
    } else {
      await CollectionManagementApi.updateCollectionManagement(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    generalAccessionNumber: undefined,
    category: undefined,
    name: undefined,
    originalName: undefined,
    coverImage: undefined,
    attachments: undefined,
    materialType: undefined,
    actureQuantity: undefined,
    quality: undefined,
    quantity: undefined,
    grade: undefined,
    sourceExtention: undefined,
    source: undefined,
    conditionStatus: undefined,
    preservationStatus: undefined,
    entryDate: undefined,
    remarks: undefined,
    paintingLength: undefined,
    paintingWidth: undefined,
    paintingHeight: undefined,
    mountingLength: undefined,
    mountingWidth: undefined,
    mountingHeight: undefined,
    subjectMatter: undefined,
    techniqueStyle: undefined,
    inscriptionInfo: undefined,
    shape: undefined,
    knobStyle: undefined,
    sealContent: undefined,
    textType: undefined,
    textArrangement: undefined,
    borderFeature: undefined,
    sealSize: undefined
  }
  formRef.value?.resetFields()
}
</script>
